package com.example.smartgreenhouse.service.impl;

import com.example.smartgreenhouse.entity.AirTemperature;
import com.example.smartgreenhouse.mapper.AirTemperatureMapper;
import com.example.smartgreenhouse.service.AirDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Configuration
public class AirTemperatureService implements AirDataService {


    private final AirTemperatureMapper airTemperatureMapper;
    @Autowired
    public AirTemperatureService(AirTemperatureMapper airTemperatureMapper) {
        this.airTemperatureMapper = airTemperatureMapper;
    }

    @Override
    public Map<String, Object> saveAirData(int temperature, int changValue,float changeRate ,LocalDateTime collectTime) {
        Map<String,Object> response = new HashMap<>();
        //创建对象保存数据
        AirTemperature airTemperature = new AirTemperature();
        airTemperature.setTemperature(temperature);
        airTemperature.setTemperature_change_value(changValue);
        airTemperature.setTemperature_change_rate(changeRate);
        airTemperature.setCollect_time(collectTime);
        airTemperature.setCreate_time(LocalDateTime.now());
        //使用Mapper保存到数据库
        airTemperatureMapper.insert(airTemperature);
        // 返回成功响应
        response.put("status", "success");
        response.put("message", "Data saved successfully.");
        response.put("timestamp", LocalDateTime.now());
        return response;
    }
}
